package com.egao.appo.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.appo.biz.MessageBiz;
import com.egao.appo.entity.Message;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-08-25 16:22:48
 */
@Repository
public class MessageBizImpl extends BaseDao implements MessageBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Message message, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.receiver_appo_id");
        sql.append(",m.content,m.receiver_user_id,m.type,m.send_user_id");
        sql.append(",m.remark,m.state,m.order_num,m.create_time");
        sql.append(",m.create_user,m.update_time,m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(",send.nickname sendUserName,receiver.nickname receiverUserName, appo.name receiverAppoName ");
        sql.append(" from fx_message m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" left join sys_user receiver on receiver.user_id=m.receiver_user_id ");
        sql.append(" left join sys_user send on send.user_id=m.send_user_id ");
        sql.append(" left join fx_appointments appo on appo.id=m.receiver_appo_id ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and m.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(message)) {
//			if(Utils.isNotEmpty(message.getName())){
//				sql.append(" and instr(m.name,:name)>0 ");
//				map.put("name", message.getName());
//			}
            if (Utils.isNotEmpty(message.getSort()) && Utils.isNotEmpty(message.getOrder())) {
                sql.append(" order by m." + message.getSort() + " " + message.getOrder());
            } else {
                sql.append(" order by m.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Message> list(Message message, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.receiver_appo_id");
        sql.append(",m.content,m.receiver_user_id,m.type,m.send_user_id");
        sql.append(",m.remark,m.state,m.order_num,m.create_time");
        sql.append(",m.create_user,m.update_time,m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(",send.nickname sendUserName,receiver.nickname receiverUserName, appo.name receiverAppoName ");
        sql.append(" from fx_message m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" left join sys_user receiver on receiver.user_id=m.receiver_user_id ");
        sql.append(" left join sys_user send on send.user_id=m.send_user_id ");
        sql.append(" left join fx_appointments appo on appo.id=m.receiver_appo_id ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and m.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(message)) {
//			if(Utils.isNotEmpty(message.getName())){
//				sql.append(" and instr(m.name,:name)>0 ");
//				map.put("name", message.getName());
//			}
            if (Utils.isNotEmpty(message.getSort()) && Utils.isNotEmpty(message.getOrder())) {
                sql.append(" order by m." + message.getSort() + " " + message.getOrder());
            } else {
                sql.append(" order by m.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Message examinedMessage(Message message) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.receiver_appo_id");
        sql.append(",m.content,m.receiver_user_id,m.type,m.send_user_id");
        sql.append(",m.remark,m.state,m.order_num,m.create_time");
        sql.append(",m.create_user,m.update_time,m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(",send.nickname sendUserName,receiver.nickname receiverUserName, appo.name receiverAppoName ");
        sql.append(" from fx_message m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" left join sys_user receiver on receiver.user_id=m.receiver_user_id ");
        sql.append(" left join sys_user send on send.user_id=m.send_user_id ");
        sql.append(" left join fx_appointments appo on appo.id=m.receiver_appo_id ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 ");
				sql.append(" and  m.receiver_appo_id=:receiver_appo_id");
				map.put("receiver_appo_id", message.getReceiverAppoId()+"");
        if(Utils.isNotEmpty(message.getType())){
            sql.append(" and m.type=:type ");
            map.put("type", message.getType());
        }
        sql.append(" order by m.create_time desc ");
        sql.append(" limit 1 ");
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public Message getById(Message message) {
        RowMapper<Message> rm = new BeanPropertyRowMapper<Message>(Message.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.receiver_appo_id");
        sql.append(",m.content,m.receiver_user_id,m.type,m.send_user_id");
        sql.append(",m.remark,m.state,m.order_num,m.create_time");
        sql.append(",m.create_user,m.update_time,m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(",send.nickname sendUserName,receiver.nickname receiverUserName, appo.name receiverAppoName ");
        sql.append(" from fx_message m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" left join sys_user receiver on receiver.user_id=m.receiver_user_id ");
        sql.append(" left join sys_user send on send.user_id=m.send_user_id ");
        sql.append(" left join fx_appointments appo on appo.id=m.receiver_appo_id ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 and m.id=:id ");
        map.put("id", message.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Message message, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into fx_message (");
        sql.append("organization_id,receiver_appo_id,content,receiver_user_id");
        sql.append(",type,send_user_id,remark,state");
        sql.append(",order_num,create_user,create_time) values ( ");
        sql.append(":organization_id,:receiver_appo_id,:content,:receiver_user_id");
        sql.append(",:type,:send_user_id,:remark,:state");
        sql.append(",:order_num,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("receiver_appo_id", message.getReceiverAppoId() == null ? null : message.getReceiverAppoId() + "");
        map.put("content", message.getContent());
        map.put("receiver_user_id", message.getReceiverUserId() == null ? null : message.getReceiverUserId() + "");
        map.put("type", message.getType());
        map.put("send_user_id", message.getSendUserId() == null ? null : message.getSendUserId() + "");
        map.put("remark", message.getRemark());
        map.put("state", message.getState());
        map.put("order_num", message.getOrderNum() == null ? null : message.getOrderNum() + "");
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Message message, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update fx_message set ");
        sql.append("receiver_appo_id=:receiver_appo_id,content=:content,receiver_user_id=:receiver_user_id ");
        sql.append(",type=:type,send_user_id=:send_user_id,remark=:remark ");
        sql.append(",state=:state,order_num=:order_num,update_time=now() ");
        sql.append(",update_user=:update_user where id=:id ");
		map.put("receiver_appo_id", message.getReceiverAppoId() == null ? null : message.getReceiverAppoId() + "");
		map.put("content", message.getContent());
		map.put("receiver_user_id", message.getReceiverUserId() == null ? null : message.getReceiverUserId() + "");
		map.put("type", message.getType());
        map.put("send_user_id", message.getSendUserId() == null ? null : message.getSendUserId() + "");
        map.put("remark", message.getRemark());
        map.put("state", message.getState());
        map.put("order_num", message.getOrderNum() == null ? null : message.getOrderNum() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", message.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(Message message, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (message.getId().length() > 0) {
            for (String ID : message.getId().split(",")) {
                map = new HashMap<String, String>();
                if (ID == null || ID.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update fx_message set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
